package service;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import dao.PlaceDao;

public class PlaceOrderService {
	private List list;
	private Map<String, Object> request, session;
	
	//用户预约业务逻辑 传入参数为用户的id 和 订单的 id
	public Boolean place(int userID,int orderID) {
		//更新订单用户暂时为该发起者 并更新订单状态到1-等待家教确认
		//System.out.println("进入了update函数");
		String hql = "update Order o set o.costomerID = "+userID+"  ,o.status = 1 where o.orderID = "+orderID;
		//System.out.println("更新语句为："+hql);
		PlaceDao dao = new PlaceDao();
		dao.update(hql);
		return true;
	}
	//用户取消预约
	public Boolean cancelOrder(int orderID) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		String hql = "update Order o set o.costomerID = null,o.status = 0 where o.orderID = "+orderID;
		PlaceDao dao = new PlaceDao();
		dao.update(hql);
		request.put("tip", "已取消该申请！");
		return true;
	}
	//用户发起结束订单
	public Boolean finishOrder(int orderID) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		String hql = "update Order o set o.status = 3 where o.orderID = "+orderID;
		PlaceDao dao = new PlaceDao();
		dao.update(hql);
		request.put("tip", "该订单已结束！");
		return true;
	}
	//用户得到该账户下待处理和进行中的订单 订单状态为1和2
	public Boolean getOrders(int id) {
		String hql = "from Order as user where costomerID = "+id+" and status in(1,2,3)";
		PlaceDao dao = new PlaceDao();
		list = dao.getOrders(hql);
		if(list.isEmpty()) {
			return false;
		}
		return true;
	}
		
	//家教获得自己发布的订单信息
	public Boolean getOrdersForTutor(int id) {
		String hql = "from Order as user where promulgatorID = "+id+" and status in(0,1,2,3)";
		PlaceDao dao = new PlaceDao();
		list = dao.getOrders(hql);
		if(list.isEmpty()) {
			return false;
		}
		return true;
	}
	
	//用户获得发布的招聘信息情况
	public Boolean getEmpForUser(int id) {
		String hql = "from Employment as emp where costomerID = "+id+" and status in(0,1,2)";
		PlaceDao dao = new PlaceDao();
		list = dao.getOrders(hql);
//		if(list.isEmpty()) {
//			return false;
//		}
		return true;
	}
	//家教获得自己名下的招聘信息情况
	public Boolean getEmpForTutor(int id) {
		String hql = "from Employment as emp where promulgatorID = "+id+" and status in(1,2,3)";
		PlaceDao dao = new PlaceDao();
		list = dao.getOrders(hql);
//		System.out.println("家教招聘信息的数目："+list.size());
//		if(list.isEmpty()) {
//			return false;
//		}
		return true;
	}
	
	public List getList() {
		return this.list;
	}
}
